<template>
  <edit-dialog :centerDialogVisible="visible" tilte="添加外部人员" width="800" @Cancel="visible=false" @Submit="handleSubmit">
    <template #content>
      <div class="add-outer-user">
        <!--        <div class="add-row">-->
        <!--          <div class="left">-->
        <!--            <div class="block"></div>-->
        <!--            <span>添加人员</span>-->
        <!--          </div>-->
        <!--          <div class="right">-->
        <!--            <el-icon color="#1caad5" size="12px">-->
        <!--              <Plus/>-->
        <!--            </el-icon>-->
        <!--          </div>-->
        <!--        </div>-->
        <OuterUser :form="form" :options="options" ref="outerUserRef" :notRequireFile="notRequireFile"/>
      </div>
    </template>
  </edit-dialog>
</template>

<script>
import OuterUser from "@/views/construction/project-application/componens/OuterUser";
import EditDialog from "@/components/editDialog/index.vue";

export default {
  name: "AddOuterUser",
  components: {OuterUser, EditDialog},
  props: {
    options: {
      type: Object,
      default: () => ({})
    },
    operators: {
      type: Array,
      default: () => []
    },
    notRequireFile: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      visible: false,
      form: {},
    }
  },
  methods: {
    open({row, resolve}) {
      this.resolve = resolve
      this.visible = true
      this.form = JSON.parse(JSON.stringify(row))
    },
    async handleSubmit() {
      try {
        await this.$refs.outerUserRef.valid()
        if (!this.form.randomId) {
          this.validUser()
          this.form.randomId = Math.ceil(Math.random() * 100000)
        }
        if (this.resolve) {
          this.resolve({...this.form})
        }
        this.visible = false
      } catch (e) {
        this.$message.error(e.message || e)
      }
    },
    validUser() {
      let find = this.operators.find(item => item.idCard === this.form.idCard &&  item.specialJobType === this.form.specialJobType)
      if (find) {
        throw new Error("同一作业类型无法添加相同人员")
      }
    }
  }
}
</script>

<style lang="less" scoped>
.add-outer-user {
  overflow: auto;
  padding-right: 10px;

  .add-row {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .block {
        height: 15px;
        width: 6px;
        background: #1caad5;
        margin-right: 10px;
      }
    }

    .right {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 16px;
      height: 16px;
      border-radius: 50%;
      border: 1px solid #1caad5;
      cursor: pointer;
    }
  }
}
</style>
